package com.puntodonna.fogliocassa.client.widget;

import java.util.Collection;
import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.view.client.ListDataProvider;

public abstract class AbstractDataTableWithPager<T> extends DataGrid<T> {

	protected SimplePager pager;

	protected ListDataProvider<T> dataProvider;

	protected ListHandler<T> sortHandler;

	interface TableResources extends DataGrid.Resources {
		@Override
		@Source(value = { DataGrid.Style.DEFAULT_CSS, "abstractDataTable.css" })
		DataGrid.Style dataGridStyle();
	}

	public AbstractDataTableWithPager(ListDataProvider<T> dataProvider) {
		super(50, GWT.<TableResources> create(TableResources.class));
		this.pager = new SimplePager();
		this.pager.setDisplay(this);
		this.dataProvider = dataProvider;
		this.dataProvider.addDataDisplay(this);
		this.sortHandler = new ListHandler<T>(getDataProvider().getList());
		initializeColumns();
	}

	protected abstract void initializeColumns();

	public ListDataProvider<T> getDataProvider() {
		return dataProvider;
	}

	public ListHandler<T> getSortHandler() {
		return sortHandler;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void reset() {
		getDataProvider().getList().clear();
	}

	public void refresh() {
		getDataProvider().refresh();
	}

	public void modifyItem(T item) {
		removeItem(getByKey(getDataProvider().getKeyProvider().getKey(item)));
		addItem(item, true);
	}

	public void addItem(T item) {
		addItem(item, true);
	}

	public void addItem(T item, boolean refresh) {
		getDataProvider().getList().add(item);
		if (refresh)
			refresh();
	}

	public void addItems(Collection<T> items) {
		addItems(items, true);
	}

	public void addItems(Collection<T> items, boolean refresh) {
		getDataProvider().getList().addAll(items);
		if (refresh)
			refresh();
	}

	public void removeItems(Collection<T> items) {
		removeItems(items, true);
	}

	public void removeItems(Collection<T> items, boolean refresh) {
		for (Iterator<T> iterator = items.iterator(); iterator.hasNext();) {
			T t = (T) iterator.next();
			removeItem(t);
		}
		if (refresh)
			refresh();
	}

	public void removeItem(T item) {
		removeItem(item, true);
	}

	public void removeItem(T item, boolean refresh) {
		item = getByKey(getDataProvider().getKeyProvider().getKey(item));
		boolean wasRemoved = getDataProvider().getList().remove(item);
		GWT.log("removeItem--->" + wasRemoved);
		if (refresh)
			refresh();
	}

	public void clear() {
		getDataProvider().getList().clear();
		refresh();
	}

	public T getByKey(Object key) {
		for (Iterator<T> iterator = getDataProvider().getList().iterator(); iterator.hasNext();) {
			T type = (T) iterator.next();
			if (getDataProvider().getKeyProvider().getKey(type).equals(key))
				return type;
		}
		return null;
	}
}
